package com.ittx.dzt.tiktok.service;

import cn.hutool.core.collection.CollUtil;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import open.dto.PackageDS;
import open.dto.PackageDetailDS;
import open.dto.ShipmentDispatchDto;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.ittx.dzt.tiktok.constant.TiktokApiConstant;
import com.ittx.dzt.tiktok.handler.AbstractBaseTiktokApiHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发货，美国和欧洲，卖家发货
 * <p>
 * cy.shipment.dispatch
 * /fulfillment/202309/orders/{order_id}/packages (发货，美国和欧洲，卖家发货)  https://partner.tiktokshop.com/docv2/page/6503c95456e2bb0289ee4e81?external_id=6503c95456e2bb0289ee4e81
 * </p>
 *
 * @author gywang
 * @since 2024-07-22
 */
@Slf4j
@Service
public class TiktokMarkPackagesAsShipedService extends AbstractBaseTiktokApiHandler<ShipmentDispatchDto> {
    @Override
    public String getTiktokMethod() {
        return TiktokApiConstant.MARK_ORDER_PACKAGES_SHIPED;
    }

    @Override
    public String getLocalApiMethod() {
        return open.constant.PlatformApiConstant.InboundInterface.SHIPMENT_ORDERS_DISPATCH;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, ShipmentDispatchDto shipmentDispatchDTO) {
        Result<?> validResult = checkShopCipherInfo(platformApiParamDTO);
        if (validResult.hasError()) {
            return validResult;
        }
        String url = getDefaultUrl(platformApiParamDTO.getStoreConfig());
        url = StringUtils.replace(url, "{order_id}", shipmentDispatchDTO.getSourceOrderCode());
        Map<String, Object> params = new HashMap<>();
        params.put("shop_cipher", getShopCipher(platformApiParamDTO));
        Map<String, Object> reqBody = new HashMap<>();
        if (CollUtil.isEmpty(shipmentDispatchDTO.getPacks())) {
            reqBody.put("tracking_number", shipmentDispatchDTO.getWaybillCode());
            reqBody.put("shipping_provider_id", shipmentDispatchDTO.getCarrierCode());
            if (CollUtil.isNotEmpty(shipmentDispatchDTO.getSourceLineNums())) {
                reqBody.put("order_line_item_ids", shipmentDispatchDTO.getSourceLineNums());
            }
            return this.executeTiktokApi(url, platformApiParamDTO.getStoreConfig(), params, JacksonTool.toJson(reqBody), platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.POST);
        }

        for (PackageDS pack : shipmentDispatchDTO.getPacks()) {
            reqBody.put("tracking_number", pack.getWaybillCode());
            reqBody.put("shipping_provider_id", pack.getCarrierCode());
            if (CollUtil.isNotEmpty(pack.getDetails())) {
                List<String> lines = new ArrayList<>();
                for (PackageDetailDS d : pack.getDetails()) {
                    lines.add(d.getSourceLineNum());
                }
                reqBody.put("order_line_item_ids", lines);
            }
            Result r = this.executeTiktokApi(url, platformApiParamDTO.getStoreConfig(), params, JacksonTool.toJson(reqBody), platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.POST);
            if (!r.isSuccess()) {
                return r;
            }
        }
        return Result.success();

    }
}
